<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
	"http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>Comet echo RPC</title>

		<script type="text/javascript">
			var djConfig = { isDebug: false };
		</script>
		<script type="text/javascript" src="${pageContext.request.contextPath}/dojo/dojo.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath}/behaviour.js"></script>
		<script language="JavaScript" type="text/javascript">

	function $() {
	  return document.getElementById(arguments[0]);
	}


        var EvUtil =
        {
          getKeyCode : function(ev)
          {
            var keyc;
            if (window.event)
              keyc=window.event.keyCode;
            else
              keyc=ev.keyCode;
            return keyc;
          }
        };

        var echoBehaviours =
        {
            '#phrase' : function(element)
            {
              element.setAttribute("autocomplete","OFF");
              element.onkeyup = function(ev)
              {
                  var keyc=EvUtil.getKeyCode(ev);
                  if (keyc==13 || keyc==10)
                  {
                    echoRpc($('phrase').value);
                    $('phrase').value='';
	                return false;
	              }
                  return true;
              }
            },

            '#sendB' : function(element)
            {
              element.onclick = function(event)
              {
                echoRpc($('phrase').value);
                $('phrase').value='';
                return false;
              }
            }
          };

			function setUp()
			{
				dojo.require("dojo.io.cometd");
				dojo.require("dojo.event.*");

				dojo.event.connect(cometd, "finishInit",
					function(type, data, evt, request){
						cometd.subscribe(
							"/chat",
							true,
							"echoRpcReturn"
						);
					}
				);

				cometd.init({}, "${pageContext.request.contextPath}/cometd");

				Behaviour.register(echoBehaviours);
			}

			function echoRpc(msg)
			{
                var m = { 'sender': "Stefan", 'text': msg };
                cometd.publish("/chat", m);
			}

			function echoRpcReturn(msg)
			{
				$("responses").innerHTML+=msg.timestamp+" "+msg.channel+": "+msg.data.sender+": " +msg.data.text+"<br/>";
			}

			dojo.addOnLoad(setUp());
		</script>
	</head>
	<body>

	<h1>Chat Example</h1>

    <p>This example shows how to create a channel from a Spring context and how to add a Filter to it. In this case the filter obfuscates a list of words if they are entered in the chat.</p>

    <div><input id="phrase" type="text"/> <input id="sendB" class="button" type="submit" name="join" value="Send"/>
    </div>
    <pre id="responses"></pre>

    <h3>All Java Code</h3>

    <pre>
    public class ChatFilter implements BayeuxFilter
    {
        private Set&lt;String> words = new HashSet&lt;String>();

        @Required
        public void setWords(Set&lt;String> words) {
            this.words = words;
        }

        public Object filter(Bayeux bayeux, Client from, Object o)
        {
            String s = o.toString();

            StringBuilder sb = new StringBuilder(s.length());

            StringTokenizer st = new StringTokenizer(s);
            while (st.hasMoreTokens()) {
                String token = st.nextToken();
                if (words.contains(token)) {
                    token = token.replaceAll("[euioa]", "*");
                }
                if (sb.length() != 0) {
                    sb.append(" ");
                }
                sb.append(token);
            }

            return sb.toString();
        }
    }
    </pre>

    <h3>All Spring Configuration</h3>

    <pre>
    &lt;bean id="chatFilter" class="com.polarrose.spring.bayeux.examples.chat.ChatFilter">
      &lt;property name="words">
        &lt;set>
          &lt;value>microsoft&lt;/value>
          &lt;value>tomcat&lt;/value>
          &lt;value>sprouts&lt;/value>
          &lt;value>clowns&lt;/value>
        &lt;/set>
      &lt;/property>
    &lt;/bean>

    &lt;bean id="jettyChatChannel" class="com.polarrose.spring.bayeux.impl.BayeuxChannelImpl">
      &lt;property name="name" value="/chat/jetty"/>
      &lt;property name="filters">
        &lt;list>
          &lt;ref local="chatFilter"/>
        &lt;/list>
      &lt;/property>
    &lt;/bean>
    </pre>

	</body>
</html>
